﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DAO;
using System.Collections.Generic;
using Microsoft.Phone.Tasks;

namespace BUS
{
    public class TuVungBUS
    {
        public List<TuDien> LayDanhSachTuVung()
        {
            return MyDatabase._lstTuDien;
        }
        public TuDien LayTuVungTheoID(string id)
        {
            
            for (int i = 0; i < MyDatabase._lstTuDien.Count; i++)
            {
                TuDien kqua = MyDatabase._lstTuDien[i];
                if (kqua.ID == Int32.Parse(id))
                    return kqua;
            }
            return null;
        }

        public TuDien LayTuVung(string keyWord)
        {

            for (int i = 0; i < MyDatabase._lstTuDien.Count; i++)
            {
                TuDien kqua = MyDatabase._lstTuDien[i];
                if (kqua.VnWord == keyWord)
                    return kqua;
            }
            return null;
        }

        public TuDien LayTuVungTheoID(int id)
        {
            for (int i = 0; i < MyDatabase._lstTuDien.Count; i++)
            {
                TuDien kqua = MyDatabase._lstTuDien[i];
                if (kqua.ID == id)
                    return kqua;
            }
            return null;

        }
        public List<TuDien> TimKiemTuDien(string key)
        {
            TuDienDAO tuDienDao = new TuDienDAO();
            return tuDienDao.TimKiemTuDien(key);
        }
        public List<TuDien> TimKiemTuDien(string key, int type)
        {
            TuDienDAO tuDienDao = new TuDienDAO();
            return tuDienDao.TimKiemTuDien(key, type);
        }
        public string LayVungTuDien(TuDien tudien)
        {
            for (int i = 0; i < MyDatabase._lstVungMien.Count; i++)
            {
                Vung temp = MyDatabase._lstVungMien[i];
                if (temp.ID == tudien.LocalID)
                {
                    return temp.LocalName;
                }
            }
            return "";
        }

        public void ShareTuVung(TuDien tuDien)
        {
            ShareStatusTask share = new ShareStatusTask();
            string strtemp = tuDien.VnWord +"  ("+ tuDien.EnWord + " )"+ "\n" +"Mô Tả: \n"+ tuDien.Description;
            string linkMedia =PublicAndStatic.ConvertToUri(tuDien).ToString();
            if (strtemp.Length > MyDatabase.MaxStrShare - MyDatabase.lenghStrShare - linkMedia.Length)
                strtemp = strtemp.Substring(0, MyDatabase.MaxStrShare - MyDatabase.lenghStrShare - linkMedia.Length);
            strtemp = strtemp + ".... " + linkMedia + MyDatabase.StrShare;
            share.Status = strtemp;
            share.Show();
        }

        public TuDien TimKiemTuVung_NhiPhan(string keyWord)
        {
            int index = FindIndex(keyWord, 0, keyWord.Length - 1);
            return MyDatabase._lstTuDien[index];
        }

        private int FindIndex(string strKey, int dau, int sau)
        {

            int mid = (sau - dau)/2;
            string temp = MyDatabase._lstTuDien[mid].VnWord;
            if (temp == strKey)
                return mid;
            if (temp.CompareTo(strKey) > 0)
                dau = mid;
            else
                sau = mid;
            return FindIndex(strKey, dau, sau);
        }
        public List<TuDien> PhanTichCau(string input)
        {
            List<TuDien> result = new List<TuDien>();

            // Thuat toan phan tich cau input vao thanh list cac tu co trong database
            // Sap cac tu theo thu tu tu trai qua phai cua 1 cau
            // nghia la for tu 1 --> list.count: xuat ra với dấu cách ta có đc câu ( sau khi phân tích )
            // Vế đề: câu này có thể sai nghĩa với input (--> cải tiến sau)
            TuVungBUS tuvungBUs = new TuVungBUS();
            TuDien tempTu = new TuDien();
            string tudangtim, tuconlai, tempTuConLai;
            tudangtim = ChuanHoa(input);
            tuconlai = "";

            tempTu = tuvungBUs.LayTuVung(tudangtim);
            if (tempTu != null)
                result.Add(tempTu);
            else
            {
                int indexLastSpace = FindIndexLastSpace(tudangtim);
                while (indexLastSpace != -1)
                {
                    tempTuConLai = tudangtim.Substring(indexLastSpace, tudangtim.Length - indexLastSpace);
                    tuconlai = (tempTuConLai + " " + tuconlai).Trim();
                    tudangtim = tudangtim.Substring(0, indexLastSpace);

                    tempTu = tuvungBUs.LayTuVung(tudangtim);
                    if (tempTu != null)
                    {
                        result.Add(tempTu);
                        tudangtim = tuconlai;
                        tuconlai = "";
                        indexLastSpace = tudangtim.Length;
                    }
                    else
                        indexLastSpace = FindIndexLastSpace(tudangtim);

                    if (indexLastSpace == -1 && tuconlai != "")
                    {
                        tudangtim = tuconlai;
                        tuconlai = "";
                        indexLastSpace = tudangtim.Length;
                    }
                }
            }

            return result;
        }
        #region  Ho tro xu ly chuoi
        private string ChuanHoa(string input)
        {
            string result = input.Trim();
            while (result.IndexOf("  ") != -1)
            {
                result = result.Replace("  ", " ");
            }
            return result.ToLower();
        }

        private int FindIndexLastSpace(string input)
        {
            int index = -1;
            for (int i = input.Length - 1; i > 0; i--)
            {
                if (input[i] == ' ')
                {
                    index = i;
                    return index;
                }
            }
            return -1;
        }
        #endregion      
    }
}
